//
//  UIView+UIViewProperty.m
//  FuncProgramDemo
//
//  Created by lc4y on 2017/10/11.
//  Copyright © 2017年 lc4y. All rights reserved.
//

#import "UIView+UIViewProperty.h"

@implementation UIView (UIViewProperty)

- (BOOL)isLabelOrTextFieldOrTextView {
    return ([self isKindOfClass:[UILabel class]]
    || [self isKindOfClass:[UITextView class]]
    || [self isKindOfClass:[UITextField class]]);
}

- (BOOL)isImageView {
    return [self isKindOfClass:[UIImageView class]];
}

- (BOOL)isButton {
    return [self isKindOfClass:[UIButton class]];
}

- (BOOL)isButtonOrControl {
    return ([self isButton]
           || [self isKindOfClass:[UIControl class]]);
}

- (UIView *(^)(UIColor *backgroundColor))cyBGColor {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(UIColor *backgroundColor) {
        weakSelf.backgroundColor = backgroundColor;
        return weakSelf;
    };
}

- (UIView *(^)(CGFloat alphaValue))cyAlpha {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(CGFloat alpha) {
        weakSelf.alpha = alpha;
        return weakSelf;
    };
}

-(UIView *(^)(NSInteger))cyTag{
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSInteger tag) {
        weakSelf.tag = tag;
        return weakSelf;
    };
}

- (UIView *(^)(CGRect))cyFrame {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(CGRect frame) {
        weakSelf.frame = frame;
        return weakSelf;
    };
}

- (UIView *(^)(CGRect))cyBounds {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(CGRect bounds) {
        weakSelf.bounds = bounds;
        return weakSelf;
    };
}

- (UIView *(^)(UIView *))cyAddToSuperView {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(UIView *superView) {
        [superView addSubview:weakSelf];
        return weakSelf;
    };
}

- (UIView *(^)(BOOL mask))cyClipsToBounds {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(BOOL mask) {
        weakSelf.clipsToBounds = mask;
        return weakSelf;
    };
}

- (UIView *(^)(CGFloat))cyLayerCornerRadius {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(CGFloat radius) {
        weakSelf.layer.cornerRadius = radius;
        return weakSelf;
    };
}

- (UIView *(^)(CGFloat))cyLayerBorderWidth {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(CGFloat borderWidth) {
        weakSelf.layer.borderWidth = borderWidth;
        return weakSelf;
    };
}

- (UIView *(^)(UIColor *))cyLayerBorderColor {
    __weak typeof(self) weakSelf = self;
    return ^UIView *(UIColor *borderColor) {
        weakSelf.layer.borderColor = borderColor.CGColor;
        return weakSelf;
    };
}

-(UIView *(^)(BOOL))cyUserInteractionEnabled {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(BOOL enable) {
        weakSelf.userInteractionEnabled = enable;
        return weakSelf;
    };
}

- (UIView *(^)(NSString *))cyText{
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSString *text) {
        if([weakSelf isLabelOrTextFieldOrTextView]){
            [weakSelf setValue:text forKey:@"text"];
        } else {
            
            NSLog(@"不能为当前非UILabel添加[%@ %@] 方法", NSStringFromClass([weakSelf class]), NSStringFromSelector(_cmd));
        }
        return weakSelf;
    };
}

- (UIView *(^)(NSAttributedString *))cyAttributedText {
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSAttributedString *attributedText) {
        if([weakSelf isLabelOrTextFieldOrTextView]){
            [weakSelf setValue:attributedText forKey:@"attributedText"];
        } else {
            NSLog(@"不能为当前非UILabel添加[%@ %@] 方法", NSStringFromClass([weakSelf class]), NSStringFromSelector(_cmd));
        }
        return weakSelf;
    };
}

- (UIView *(^)(UIColor *))cyTextColor{
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(UIColor *textColor) {
        if([weakSelf isLabelOrTextFieldOrTextView]){
            [weakSelf setValue:textColor forKey:@"textColor"];
        } else {
            NSLog(@"不能为当前非UILabel添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        return weakSelf;
    };
}

- (UIView *(^)(NSTextAlignment))cyTextAlignment{
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSTextAlignment textAlignment) {
        if([weakSelf isLabelOrTextFieldOrTextView]){
            [weakSelf setValue:@(textAlignment) forKey:@"textAlignment"];
        } else {
            NSLog(@"不能为当前非UILabel添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        
        return weakSelf;
    };
}

- (UIView *(^)(NSInteger))cyNumberOfLines {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSInteger numberOfLines) {
        if([weakSelf isLabelOrTextFieldOrTextView]){
            [weakSelf setValue:@(numberOfLines) forKey:@"numberOfLines"];
        } else {
            NSLog(@"不能为当前非UILabel添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        return weakSelf;
    };
}

-(UIView *(^)(NSString *))cyImage {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSString *img) {
        if([weakSelf isImageView]){
            [weakSelf setValue:[UIImage imageNamed:img]  forKey:@"image"];
        } else {
            NSLog(@"不能为当前非UIImageView添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        return weakSelf;
    };
}

-(UIView *(^)(NSString *))cyHighlightImage {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSString *highlightImg) {
        if([weakSelf isImageView]){
            [weakSelf setValue:[UIImage imageNamed:highlightImg]  forKey:@"highlightedImage"];
        } else {
            NSLog(@"不能为当前非UIImageView添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));        }
        return weakSelf;
    };
}

-(UIView *(^)(BOOL))cyHighlighted{
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(BOOL highlighted) {
        if([weakSelf isImageView]){
            [weakSelf setValue:@(highlighted)  forKey:@"highlighted"];
        } else {
            NSLog(@"不能为当前非UIImageView添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }

        return weakSelf;
    };
}

- (UIView *(^)(NSString *,UIControlState))cyTitle {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSString *title,UIControlState buttonState) {
        if([weakSelf isButton]){
            UIButton *button = (UIButton *)weakSelf;
            [button setTitle:title forState:buttonState];
        } else {
            NSLog(@"不能为当前非UIButton添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        return weakSelf;
    };
}

- (UIView *(^)(UIColor *,UIControlState))cyTitleColor {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(UIColor *titleColor,UIControlState buttonState) {
        if([weakSelf isButton]){
            UIButton *button = (UIButton *)weakSelf;
            [button setTitleColor:titleColor forState:buttonState];
        } else {
            NSLog(@"不能为当前非UIButton添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        
        return weakSelf;
    };
}

- (UIView *(^)(NSString *,UIControlState))cyButtonImage {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSString *image,UIControlState buttonState) {
        if([weakSelf isButton]){
            UIButton *button = (UIButton *)weakSelf;
            if(image){
                [button setImage:[UIImage imageNamed:image] forState:buttonState];
            }
        } else {
            NSLog(@"不能为当前非UIButton添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        
        return weakSelf;
    };
}

- (UIView *(^)(NSString *,UIControlState))cyButtonBGImage {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(NSString *image,UIControlState buttonState) {
        if([weakSelf isButton]){
            UIButton *button = (UIButton *)weakSelf;
            [button setBackgroundImage:[UIImage imageNamed:image] forState:buttonState];
        } else {
            NSLog(@"不能为当前非UIButton添加 [%@ %@] 方法", NSStringFromClass([weakSelf class]),NSStringFromSelector(_cmd));
        }
        
        return weakSelf;
    };
}

- (UIView *(^)(id, NSString *, UIControlEvents))cyAddEvents {
    
    __weak typeof(self) weakSelf = self;
    return ^UIView *(id target,NSString *eventHandle,UIControlEvents buttonEvent) {
        if([weakSelf isButtonOrControl]){
            UIButton *button = (UIButton *)weakSelf;
            //当前对象响应该方法,才会添加事件
            if([target respondsToSelector:NSSelectorFromString(eventHandle)]){
                [button addTarget:target action:NSSelectorFromString(eventHandle) forControlEvents:buttonEvent];
            } else {
                NSLog(@"响应对象无当前事件方法");
            }
        } else {
            NSLog(@"不能为当前非UIControl的子类添加改事件");
        }
        
        return weakSelf;
    };
}

@end
